import Vue from "vue";
import Vuex from "vuex";
import utils from "@/assets/js/utils"; //载入更新路由方法
Vue.use(Vuex);
var url1 = "/api";
var url2 = "https://wawa.wawabdsh.com/api";
var url3 = "http://218.75.20.10:10086";
var shareUrl1 = "https://wxtest.tofocus.cn/banksm-wx"; //测试分享地址
var shareUrl2 = "https://weixin.sm96596.com/fslm/banksm-wx"; //正式分享地址
var uploadUrl = "https://wxtest.tofocus.cn/file/v3/image/upload"; //测试上传地址
var uploadUrl2 = "https://weixin.sm96596.com/fslm/file/v3/image/upload"; //生产上传地址
const state = {
  baseUrl:
    process.env.NODE_ENV == "development"
      ? url2
      : process.env.NODE_ENV == "test"
      ? url3
      : url2, //打test包 下单接口会报baseUrl不存在
  nodeenv:process.env.NODE_ENV,//当前环境
  loadingShow:false,//全局loading
  userinfo: JSON.parse(localStorage.getItem("userinfo")), //用户信息
  // ovkLMv1UW835BqA_BRJzrnJo_xxc 朱一帆微信openid
  openid: localStorage.getItem("openid")
    ? localStorage.getItem("openid")
    : process.env.NODE_ENV == "development"
    ? "ovkLMv1UW835BqA_BRJzrnJo_xxc"
    : "",
  appToken: localStorage.getItem("appToken")
    ? localStorage.getItem("appToken")
    : "9474e850-7672-4014-b285-14b82981f945", //开发环境去从测试环境获取
  lat: "27.984107",
  lng: "120.704856",
  version: "1.1", // 版本号
  uploadUrl:
    process.env.NODE_ENV == "development"
      ? uploadUrl
      : process.env.NODE_ENV == "test"
      ? uploadUrl
      : uploadUrl2, //上传地址
  rateIcon:
    "https://wxtest.tofocus.cn/file/v2/image?file=4235.png&code=00D96F02428C3AD8E53A3A6724AEC38C",
  rateIcon2:
    "https://wxtest.tofocus.cn/file/v2/image?file=4236.png&code=C993979E415E6C8ED7784EFD6C0AEAFE",
  balanceAmt: 0, //商家金额余额
  subscribe:false,//是否关注公众号
  registeGift:false,//是否领取过礼包
  vendorInfo: null, //商家信息
  shareUrl:
    process.env.NODE_ENV == "development"
      ? shareUrl1
      : process.env.NODE_ENV == "test"
      ? shareUrl1
      : shareUrl2 //分享跳转地址
};

const mutations = {
  /**用户信息及token值*/
  SET_USERINFO(state, userinfo) {
    console.log(userinfo);
    var str = userinfo.mobile;
    var pat = /(\d{3})\d*(\d{4})/;
    if (userinfo.mobile) {
      userinfo.mobile = str.replace(pat, "$1****$2");
    }
    localStorage.setItem("userinfo", JSON.stringify(userinfo));
    state.userinfo = userinfo;
    console.log(state.userinfo);
  },
  /**用户openid*/
  SET_OPENID(state, res) {
    localStorage.setItem("openid", res.openid);
    localStorage.setItem("appToken", res.token);
    localStorage.setItem("getOpenidArr", JSON.stringify(res));
    state.openid = res.openid;
    state.registeGift = res.registeGift;
    state.subscribe = res.subscribe;
    state.appToken = res.token;
  },
  /**经纬度*/
  SET_LOCATION(state, res) {
    var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
    var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
    var speed = res.speed; // 速度，以米/每秒计
    var accuracy = res.accuracy; // 位置精度
    state.lat = latitude;
    state.lng = longitude;
  }
};

const actions = {
  /**经纬度*/
  SET_LOCATION({ commit }, res) {
    commit("SET_LOCATION", res);
  },
  /**用户信息*/
  SET_USERINFO({ commit }, userinfo) {
    commit("SET_USERINFO", userinfo);
  },
  /**用户openid*/
  SET_OPENID({ commit }, res) {
    commit("SET_OPENID", res);
  }
};

export default new Vuex.Store({
  state,
  mutations,
  actions
});
